package com.tencent.albert.weixin.driver.servlet;

import com.tencent.albert.weixin.driver.MsgManager;
import com.tencent.albert.weixin.driver.bean.ReceivedMsgInfo;
import com.tencent.albert.weixin.driver.bean.SendMsgInfo;
import com.tencent.albert.weixin.driver.dao.MsgDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by albertzhu on 15/2/12.
 */
public class DaemonServlet extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(DaemonServlet.class);

    MsgManager msgManager ;

    public String getJdbdUrl() {
        return jdbdUrl;
    }

    public void setJdbdUrl(String jdbdUrl) {
        this.jdbdUrl = jdbdUrl;
    }

    public String getJdbcUser() {
        return jdbcUser;
    }

    public void setJdbcUser(String jdbcUser) {
        this.jdbcUser = jdbcUser;
    }

    public String getJdbcPass() {
        return jdbcPass;
    }

    public void setJdbcPass(String jdbcPass) {
        this.jdbcPass = jdbcPass;
    }

    public int getInitialPoolSize() {
        return initialPoolSize;
    }

    public void setInitialPoolSize(int initialPoolSize) {
        this.initialPoolSize = initialPoolSize;
    }

    public int getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }

    String jdbdUrl;

    String jdbcUser;

    String jdbcPass;

    int initialPoolSize = 10 ;

    int maxActive = 20 ;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.jdbdUrl = config.getInitParameter("jdbdUrl");
        this.jdbcUser = config.getInitParameter("jdbcUser");
        this.jdbcPass = config.getInitParameter("jdbcPass");
        this.initialPoolSize = Integer.parseInt(config.getInitParameter("initialPoolSize"));
        this.maxActive = Integer.parseInt(config.getInitParameter("maxActive"));

        msgManager = new MsgManager();
        msgManager.initDbConnPool(jdbdUrl,jdbcUser,jdbcPass,initialPoolSize,maxActive);
        msgManager.init();
        logger.debug("init finished.");

//        Thread t = new Thread() {
//            public void run() {
//                long count = 0 ;
//                MsgDao msgDao = new MsgDao();
//                while (true) {
//                    for (int i = 0 ; i < 10; i++) {
//                        //count++;
////                        ReceivedMsgInfo msg = new ReceivedMsgInfo();
////                        msg.setReceivedContent("<xml><ToUserName><![CDATA[gh_78d3572c4f80]]></ToUserName>\n" +
////                                "<FromUserName><![CDATA[oKsEQuEerHMdwU3VvKyK7bgxe6pU]]></FromUserName>\n" +
////                                "<CreateTime>1423814195</CreateTime>\n" +
////                                "<MsgType><![CDATA[text]]></MsgType>\n" +
////                                "<Content><![CDATA["+count+"]]></Content>\n" +
////                                "<MsgId>6115235403373472005</MsgId>\n" +
////                                "</xml>");
////                        msg.setResponseContent(String.valueOf(count));
////                        MsgManager.addReceivedMsg(msg);
//
//
////                        ArrayList<SendMsgInfo> infos = new ArrayList<>();
////                        for ( int k = 0 ; k < new Random().nextInt(2); k++) {
////                            count++;
////                            SendMsgInfo msg = new SendMsgInfo();
////                            msg.setContent(count+" Test Content");
////                            msg.setError_msg("");
////                            msg.setError_code(0);
////                            msg.setStatus(0);
////                            infos.add(msg);
////                        }
////                        msgDao.saveSendedMsgInfos(infos);
//
//                        logger.debug("create test Msg "+count);
//                        try {
//                            Thread.sleep(new Random().nextInt(1000));
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                }
//            }
//        };
//        t.start();
    }


}
